package com.miukoo.aikao.admin.types.impl;

import com.miukoo.aikao.core.dtos.QuestionAnswerDto;
import com.miukoo.aikao.core.dtos.QuestionDetailDto;
import com.miukoo.aikao.core.exceptions.ApiException;
import com.miukoo.aikao.core.pojos.Question;
import com.miukoo.aikao.core.pojos.QuestionAnswer;
import com.miukoo.aikao.core.pojos.QuestionChooseDto;
import com.miukoo.aikao.core.types.TypeInterface;
import com.miukoo.aikao.qmanage.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 多选题的实现类
 * @Author miukoo<miukoo@126.com>
 * @Date 2020/11/25  
 * @Version 1.0
 */
@Component
public class CheckBoxType implements TypeInterface {

    @Autowired
    QuestionService questionService;

    @Override
    public void saveCkeck(QuestionDetailDto dto) {
        String no = "【多选题】"+dto.getContent();
        List<QuestionAnswerDto> answerList = dto.getAnswerList();
        if(answerList==null || answerList.size() < 2){
            throw new ApiException(1,no+"至少有两个答案！");
        }
        int trueCount = checkAnswerNull(answerList, no);
        if(trueCount<2){
            throw new ApiException(1,no+"至少勾选两个正确答案！");
        }
    }

    /**
     * 选择
     * @param repoId        题库
     * @param type          题类型
     * @param level         难度
     * @param size          多少随机题
     * @param answerCount   最多多少个选项
     * @param score 得分
     * @return
     */
    @Override
    public List<QuestionChooseDto> chooseQuestion(Long repoId, Short type, Short level, Short size, Short answerCount, Short score) {
        List<Question> questions = questionService.listByRandom(repoId, type, level, null, size);
        List<QuestionChooseDto> all = new ArrayList<>();
        if(questions!=null){
            questions.forEach(question->{
                List<QuestionAnswer> newList = new ArrayList<>();
                List<QuestionAnswer> answers = question.getAnswers();
                int count=0,aSize=answers.size();
                for (int i = 0; i < aSize; i++) {
                    int index = getByRandom(answers.size());
                    QuestionAnswer temp = answers.get(index);
                    answers.remove(index);
                    if(newList.size() < answerCount) {
                        if (temp.getIsRight()){
                            count++;
                        }
                        newList.add(temp);
                    }else if(count==0){ // 没有正确答案,查找剩余的数据中正确的答案
                        if (temp.getIsRight()){
                            count++;
                            newList.add(temp);
                            break;
                        }
                    }
                }
                question.setAnswers(newList);
                QuestionChooseDto dto = new QuestionChooseDto();
                BeanUtils.copyProperties(question, dto);
                dto.setScore(score);
                all.add(dto);
            });
        }
        return all;
    }

    private int getByRandom(int maxSize){
        return (int) (Math.random()*maxSize);
    }

}
